﻿/*****************************************************************
 *@Copyright all rights reserved © 2014 Yuga
 *@author YUga studio
 *Contact: zengkang007@gmail.com
 *****************************************************************/
package com.oplink.webbase.modules.invoicing.service;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.oplink.webbase.common.persistence.Page;
import com.oplink.webbase.modules.invoicing.entity.Costomer;
import com.oplink.webbase.modules.invoicing.dao.CostomerDao;



/**
 * 告警Service
 * @author YUga studio
 * @version 2015-01-12
 */
@Component
@Transactional
public class CostomerService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(CostomerService.class);
	
	@Autowired
	private CostomerDao costomerDao;
	
	public Costomer get(Long id) {
		return costomerDao.findOne(id);
	}
	
	public Page<Costomer> find(Page<Costomer> page, Costomer costomer) {
		DetachedCriteria dc = costomerDao.createDetachedCriteria();
		//添加查询条件
		if (null != costomer.getCustomerId()
				&& StringUtils.isNotEmpty(costomer.getCustomerId().toString())) {
			dc.add(Restrictions.like("customerId", "%"+costomer.getCustomerId()+"%"));
		}
        if (StringUtils.isNotEmpty(costomer.getCustomerName())){
			dc.add(Restrictions.like("customerName", "%"+costomer.getCustomerName()+"%"));
		}
        if (StringUtils.isNotEmpty(costomer.getAddress())){
			dc.add(Restrictions.like("address", "%"+costomer.getAddress()+"%"));
		}
		dc.add(Restrictions.eq(Costomer.DEL_FLAG, Costomer.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return costomerDao.find(page, dc);
	}
	
	/**
	  * @function 创建查询
	  **/
	public Page<Costomer> getUserCostomer(Page<Costomer> page, Costomer costomer) {
		DetachedCriteria dc = costomerDao.createDetachedCriteria();
		//添加查询条件
		if (null != costomer.getCustomerId()
				&& StringUtils.isNotEmpty(costomer.getCustomerId().toString())) {
			dc.add(Restrictions.like("customerId", "%"+costomer.getCustomerId()+"%"));
		}
        if (StringUtils.isNotEmpty(costomer.getCustomerName())){
			dc.add(Restrictions.like("customerName", "%"+costomer.getCustomerName()+"%"));
		}
        if (StringUtils.isNotEmpty(costomer.getAddress())){
			dc.add(Restrictions.like("address", "%"+costomer.getAddress()+"%"));
		}
		dc.add(Restrictions.eq(Costomer.DEL_FLAG, Costomer.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return costomerDao.find(page, dc);
	}
	
	public Costomer getCostomer(Long id) {
		return costomerDao.findOne(id);
	}

	public void saveCostomer(Costomer entity) {
		costomerDao.save(entity);
	}
	
	public void deleteCostomer(Long id) {
		costomerDao.delete(id);
	}
	
	@Autowired
	public void setCostomerDao(CostomerDao costomerDao) {
		this.costomerDao = costomerDao;
	}	
}
